<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2.1_react父子组件生命周期(旧)</title>
</head>

<body>
    <div id="test"></div>
    <script type="text/javascript"
        src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/react/18.2.0/umd/react.production.min.js"></script>
    <script type="text/javascript"
        src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
    <script type="text/javascript"
        src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/babel-standalone/6.26.0/babel.min.js"></script>

    <script type="text/babel">
        /**
         * /*
                1.初始化阶段：由ReactDoM.render()触发---初次渲染
                    1. constructor()
                    2. getDerivedStateFromProps
                    3. render()
                    4. componentDidMount() ==> 常用

                2.更新阶段：由组件内部this.setSate()或父组件重新render触发
                    1. getDerivedStateFromProps
                    2. shouldComponentUpdate()
                    3. render()
                    4. getSnapshotBeforeUpdate
                    5. componentDidUpdate()

                3.卸载组件:由ReactDOM.unmountComponentAtNode()触发
                    1. componentWillUnmount()  ==> 常用
         * 
         *  
         */
        // 父组件A
        class A extends React.Component {
            state = {carName: '奔驰'}
            changeCar = () => {
                this.setState({carName: '奥拓'})
            }
            render() {
                return (
                    <div>
                        <div>我是A组件</div>
                        <button onClick={this.changeCar}>换车</button>
                        <B carName={this.state.carName}/>
                    </div>
                   
                )
            }
        }
        // 子组件B
        class B extends React.Component {
            // 挂载时触发..
            constructor(props){
                console.info('B - constructor...')
                super(props)
            }
            // 父组件render
            componentWillReceiveProps(props){
                console.info('B--componentWillReceiveProps 组件将要接收生命周期钩子...第一次不算,以后的才是...', props)
            }
            // 父组件render
            shouldComponentUpdate(){
                console.info('B--shouldComponentUpdate..')
                return true
            }
            // 父组件render
            componentWillUpdate(){
                console.info('B--componentWillUpdate..')
            }
            // 挂载时触发..
            componentWillMount(){
                console.info('B - componentWillMount..')
            }
           // 挂载时触发..
            componentDidMount(){
                console.info('B - componentDidMount..组件已经挂载..')
            }
             // 父组件render
            componentDidUpdate(){
                console.info('B--componentDidUpdate..')
            }
            componentWillUnmount(){
                console.info('B - componentWillUnmount..组件即将被卸载..')
            }

            render() {
                console.info('B-render..')
                return (
                    <div>
                        <div>我是B组件, 接收到的车是:{this.props.carName}</div>
                    </div>
                )
            }
        }

        // 创建组件
        class Life extends React.Component {

            constructor(props) {
                console.log("构造器Count-constructor...")
                super(props)
                // 初始化状态
                this.state = { count: 0 }
            }

            //组件将要挂载
            componentWillMount() {
                console.log("Count-componentWillMount...")
            }

            // 组件挂载完毕
            componentDidMount() {
                console.log("Count-componentDidMount...")
            }

            // 组件将要卸载
            componentWillUnmount() {
                console.log("Count-componentWillUnmount...")
            }

            //开关,返回true or false  如果返回false则render方法不起作用
            shouldComponentUpdate() {
                console.info("Count-shouldComponentUpdate...")
                return true
            }

            componentWillUpdate() {
                console.info("Count-componentWillUpdate...")
            }

            componentDidUpdate() {
                console.info("Count-componentDidUpdate...")
            }
            // setState()更新流程:  shouldComponentUpdate -> componentWillUpdate -> render -> componentDidUpdate -> componentWillUnmount

            // render挂载
            render() {
                const { count } = this.state
                console.log("Count-render...")
                return (
                    <div>
                        <h2>当前求和为:{count}</h2>
                        <button onClick={this.add}>点击+1</button>&nbsp;&nbsp;
                        <button onClick={this.death}>卸载组件</button>
                    </div>
                )
            }

            // 加1按钮的回调
            add = () => {
                const { count } = this.state
                this.setState({ count: count + 1 })
            }

            death = () => {
                ReactDOM.unmountComponentAtNode(document.getElementById("test"))
            }
        }
        ReactDOM.render(<A />, document.getElementById("test"))
    </script>
</body>

</html>